package cn.caipengbo.helloandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.caipengbo.bean.ShopInfo;

public class ListViewActivity extends AppCompatActivity {
    private ListView listView;
    private List<ShopInfo> shopInfoList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        listView = (ListView) findViewById(R.id.listview);
        /*
        String[] data = {"刘德华", "赵本山", "陈冠希", "马德华", "刘德华",
                "赵本山", "陈冠希", "马德华", "刘德华", "赵本山", "陈冠希", "马德华"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_array_item, data);
        listView.setAdapter(adapter);
        */
//        List<Map<String, Object>> data = new ArrayList<>();
//        Map<String, Object> map = new HashMap<>();
//        map.put("icon", R.drawable.f1);
//        map.put("name", "name1");
//        map.put("content", "content1");
//        data.add(map);
//        map = new HashMap<>();
//        map.put("icon", R.drawable.f2);
//        map.put("name", "name2");
//        map.put("content", "content2");
//        data.add(map);
//        map = new HashMap<>();
//        map.put("icon", R.drawable.f3);
//        map.put("name", "name3");
//        map.put("content", "content3");
//        data.add(map);
//        map = new HashMap<>();
//        map.put("icon", R.drawable.f4);
//        map.put("name", "name4");
//        map.put("content", "content4");
//        data.add(map);
//        map = new HashMap<>();
//        map.put("icon", R.drawable.f5);
//        map.put("name", "name5");
//        map.put("content", "content5");
//        data.add(map);
//        map = new HashMap<>();
//        map.put("icon", R.drawable.f6);
//        map.put("name", "name6");
//        map.put("content", "content6");
//        data.add(map);
//        map = new HashMap<>();
//        map.put("icon", R.drawable.f7);
//        map.put("name", "name7");
//        map.put("content", "content7");
//        data.add(map);
//        map = new HashMap<>();
//        map.put("icon", R.drawable.f8);
//        map.put("name", "name8");
//        map.put("content", "content8");
//        data.add(map);
//        map = new HashMap<>();
//        map.put("icon", R.drawable.f9);
//        map.put("name", "name9");
//        map.put("content", "content9");
//        data.add(map);
//        map = new HashMap<>();
//        map.put("icon", R.drawable.f10);
//        map.put("name", "name10");
//        map.put("content", "content10");
//        data.add(map);
//        String[] from = {"icon", "name", "content"};
//        int[] to = {R.id.item_icon, R.id.item_name, R.id.item_content};
//
//        SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.listview_simple_item, from, to);
//        listView.setAdapter(simpleAdapter);

        shopInfoList = new ArrayList<>();
        shopInfoList.add(new ShopInfo(R.drawable.f1, "name1", "content1"));
        shopInfoList.add(new ShopInfo(R.drawable.f2, "name2", "content2"));
        shopInfoList.add(new ShopInfo(R.drawable.f3, "name3", "content3"));
        shopInfoList.add(new ShopInfo(R.drawable.f4, "name4", "content4"));

        // Adpter 将 数据 与 布局 整合
        class ShopAdpter extends BaseAdapter {

            @Override
            public int getCount() {
                return shopInfoList.size();
            }

            @Override
            public Object getItem(int position) {
                return shopInfoList.get(position);
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }
            // convertView 用来缓存加载好的布局
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view;
                // 简单优化，提升ListView的效率
                if (convertView == null) {
                    view = View.inflate(ListViewActivity.this, R.layout.listview_simple_item, null);
                } else {
                    view = convertView;
                }

                ImageView icon = (ImageView) view.findViewById(R.id.item_icon);
                TextView name = (TextView) view.findViewById(R.id.item_name);
                TextView content = (TextView) view.findViewById(R.id.item_content);

                ShopInfo shopInfo = shopInfoList.get(position);

                icon.setImageResource(shopInfo.getIconId());
                name.setText(shopInfo.getName());
                content.setText(shopInfo.getContent());
                return view;
            }
        }

        listView.setAdapter(new ShopAdpter());
    }
}
